/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.dialog;


import cn.ac.nercis.pes.community.support.controls.JFXDialog;
import cn.ac.nercis.pes.community.support.event.ConfirmCallback;
import cn.ac.nercis.pes.community.utils.GuiStore;
import cn.ac.nercis.pes.community.utils.IconUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import lombok.Getter;
import lombok.Setter;

import java.util.Objects;

/**
 * 消息框组件
 * @author zpy
 * @version v1.0.0
 */
public class AlertDialog {
    private final JFXDialog dialog;
    private final PesDialogLayout layout;

    private AlertMode mode = AlertMode.ALERT;
    private AlertType type = AlertType.INFO;

    @Getter
    private String title;
    @Getter
    @Setter
    private String content;
    @Getter
    @Setter
    private StackPane dialogContainer;
    private ConfirmCallback callback;

    public AlertDialog(){
        layout = new PesDialogLayout();
        layout.getBodyStyleClass().add("pes-layout-body");
        dialog = new JFXDialog();
        dialogContainer = GuiStore.getRoot();
    }

    private AlertDialog(Builder builder){
        this();
        mode = builder.mode;
        type = builder.type;
        title = builder.title;
        content = builder.content;
        dialogContainer = builder.dialogContainer;
    }

    public void show(){
        show(null);
    }

    public void show(ConfirmCallback callback){
        this.render();
        this.callback = callback;
        this.dialog.show();
    }

    private void render(){
        this.dialog.setDialogContainer(dialogContainer);
        this.dialog.setOverlayClose(mode != AlertMode.CONFIRM);
        this.renderTitle();
        this.renderContent();
        this.renderAction();
        this.dialog.setContent(layout);
    }

    private void renderTitle(){
        var pane = new HBox();
        pane.setAlignment(Pos.CENTER_LEFT);
        pane.setPadding(new Insets(0,0,0,20));
        var titleLabel = new Label(this.title);
        titleLabel.getStyleClass().add("page-dialog-title");
        pane.getChildren().add(titleLabel);
        layout.setHeading(pane);
    }

    private void renderContent(){
        HBox container = new HBox();
        container.getStyleClass().add("alert-container");
        container.setAlignment(Pos.CENTER_LEFT);
        container.setSpacing(10);
        Label iconLabel = new Label();
        iconLabel.setContentDisplay(ContentDisplay.CENTER);
        if(type == AlertType.WARN){
            iconLabel.getStyleClass().addAll("alert-warn-icon");
            iconLabel.setGraphic(IconUtils.getGlyph("message-warn-icon","message-alert-icon"));
        }else if(type == AlertType.ERROR){
            iconLabel.getStyleClass().addAll("alert-error-icon");
            iconLabel.setGraphic(IconUtils.getGlyph("message-error-icon","message-alert-icon"));
        }else {
            iconLabel.getStyleClass().addAll("alert-success-icon");
            iconLabel.setGraphic(IconUtils.getGlyph("message-success-icon","message-alert-icon"));
        }
        Label contentLabel = new Label(content);
        contentLabel.getStyleClass().add("alert-content");
        container.getChildren().addAll(iconLabel,contentLabel);
        layout.setBody(container);
    }

    private void renderAction(){
        Button yesButton = new Button("确认");
        yesButton.setOnAction(event -> {
            this.dialog.close();
            if(Objects.nonNull(callback)){
                callback.action(true);
            }
        });
        yesButton.getStyleClass().addAll("btn","btn-save");
        if(mode == AlertMode.CONFIRM){
            Button noButton = new Button("取消");
            noButton.setOnAction(event -> {
                this.dialog.close();
                if(Objects.nonNull(callback)){
                    callback.action(false);
                }
            });
            noButton.getStyleClass().addAll("btn","btn-back");
            layout.setActions(yesButton,noButton);
        }else{
            layout.setActions(yesButton);
        }
    }

    public static Builder builder(){
        return new Builder();
    }

    public static final class Builder {
        private AlertMode mode;
        private AlertType type;
        private String title;
        private String content;
        private StackPane dialogContainer;

        private Builder(){
            mode = AlertMode.ALERT;
            type = AlertType.INFO;
            dialogContainer = GuiStore.getRoot();
        }

        public Builder withMode(AlertMode mode){
            this.mode = mode;
            return this;
        }

        public Builder withType(AlertType type){
            this.type = type;
            return this;
        }

        public Builder withTitle(String title){
            this.title = title;
            return this;
        }

        public Builder withContent(String content){
            this.content = content;
            return this;
        }

        public Builder withContainer(StackPane dialogContainer){
            this.dialogContainer = dialogContainer;
            return this;
        }

        public AlertDialog build(){
            return new AlertDialog(this);
        }
    }
}